package com.dy.course.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dy.course.common.BaseException;
import com.dy.course.common.BaseService;
import com.dy.course.common.Constant;
import com.dy.course.entity.AppBean;
import com.dy.course.mapper.AppMapper;
import com.dy.course.model.App;
import com.dy.course.service.IAppService;
import com.dy.course.util.DateUtil;

import tk.mybatis.mapper.entity.Example;

@Service
public class AppService extends BaseService<App> implements IAppService{
	@Autowired
	private AppMapper app;

	@Override
	public List<App> selectApp(AppBean ab) {
		logger.info("【查询最新版本】");
		Example example = new Example(App.class);
		example.createCriteria().andEqualTo("type",ab.getType());
		example.setOrderByClause("time desc");
		List<App> listApp = app.selectByExample(example);
		return listApp;
	}

	@Override
	public void insertApp(AppBean ab) {
		logger.info("【跟新最新版本】,参数信息:ab=" + JSON.toJSONString(ab));
		App a = new App();
		BeanUtils.copyProperties(ab, a);
		a.setTime(DateUtil.now());
		int count = app.insertSelective(a);
		if(count < 1){
			logger.error("【】数据库异常,版本跟新失败,ab=",JSON.toJSONString(ab));
			throw new BaseException(Constant.Code.DELETE_ERROR, "数据库异常,版本跟新失败", null);
		}
	}
}
